//
//  ElectricalDatePickerview.swift
//  ezyform
//
//  Created by MoennigQ on 24/9/20.
//  Copyright © 2020 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class ElectricalDatePickerview: UIView {
    var doneClosure: ((_ dateStr: String )->Void)!
    //
    let datePicker = UIPickerView()
    let pickerViewCancelBtn = BaseButton()
    let pickerViewDoneBtn = BaseButton()
    //
    let textfiledHeight: CGFloat = 40
    //
    var currentYear: Int = 0
    var currentMonth: Int = 0
    var currentDay: Int = 0
    //
    var selectYear: String = ""
    var selectMonth: String = ""
    var selectDay: String = ""
    //
    var yearsForPicker: [String] = []
    var monthTitlesForYear:[String] = []
    var monthValuesForYear:[String] = []
    //for code
    override init(frame: CGRect) {
        super.init(frame: frame)
        initPickerData()
        setUpView()
    }
    //for storyboard
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    private func setUpView() {
        //
        self.backgroundColor = UIColor.black.withAlphaComponent(0.8)
        
        self.addSubview(datePicker)
        datePicker.backgroundColor = .white
        datePicker.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.safeAreaLayoutGuide.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(200)
        }
        let btnBackView = UIView()
        btnBackView.backgroundColor = UIColor(red: 239 / 255, green: 239 / 255, blue: 239 / 255, alpha: 1.0)
        self.addSubview(btnBackView)
        btnBackView.snp.makeConstraints { (make) in
            make.bottom.equalTo(datePicker.snp.top)
            make.left.right.equalToSuperview()
            make.height.equalTo(50)
        }
        pickerViewCancelBtn.setFont(R.font.interSemiBold(size: 15))
        pickerViewCancelBtn.setTitle("Cancel", for: UIControl.State.normal)
        pickerViewCancelBtn.setTitleColor(UIColor(hexStr: Constant.ThemeColorHexStr), for: UIControl.State.normal)
        btnBackView.addSubview(pickerViewCancelBtn)
        pickerViewCancelBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.bottom.equalToSuperview()
            make.width.equalTo(80)
        }
        pickerViewDoneBtn.setFont(R.font.interSemiBold(size: 15))
        pickerViewDoneBtn.setTitle("Done", for: UIControl.State.normal)
        pickerViewDoneBtn.setTitleColor(UIColor(hexStr: Constant.ThemeColorHexStr), for: UIControl.State.normal)
        btnBackView.addSubview(pickerViewDoneBtn)
        pickerViewDoneBtn.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(80)
        }
        //
        pickerViewDoneBtn.addTarget(self, action: #selector(donePicker), for: UIControl.Event.touchUpInside)
        pickerViewCancelBtn.addTarget(self, action: #selector(cancelPicker), for: UIControl.Event.touchUpInside)
        datePicker.delegate = self
        datePicker.dataSource = self
        
        self.initPickerData()
    }
    func initPickerData(){
        let calendar: Calendar = Calendar.current
        self.currentYear = calendar.component(.year, from: Date())
        self.currentMonth = calendar.component(.month, from: Date())
        self.currentDay = calendar.component(.day, from: Date())
        yearsForPicker = ["\(currentYear - 1)", "\(currentYear + 0)", "\(currentYear + 1)"]
        monthTitlesForYear = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
        monthValuesForYear = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
        setPickerDefaultData()
    }
    func setPickerDefaultData(){
        self.selectYear = yearsForPicker[1]
        self.selectMonth = monthValuesForYear[self.currentMonth - 1]
        if currentDay < 10 {
            self.selectDay = "0\(currentDay)"
        }else{
            self.selectDay = "\(currentDay)"
        }
    }
    @objc func cancelPicker(){
        self.doneClosure("")
    }
    @objc func donePicker(){
        let dateStr: String =  self.selectDay + "/" + self.selectMonth + "/" + self.selectYear
        self.doneClosure(dateStr)
    }
}
extension ElectricalDatePickerview: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        
        if component == 0 {//day
            let days: Int = DateUtil().howManyDaysOfMonth(inThisYear: Int(self.selectYear)!, withMonth: Int(selectMonth)!)
            
            return days
        }else if component == 1{//month
            return self.monthTitlesForYear.count
        }else {//year
            return self.yearsForPicker.count
            
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return APPConfig.WidthForPortrait / 3
    }
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 40
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {//day
            if row + 1 < 10 {
                return "0\(row + 1)"
            }else{
                return "\(row + 1)"
            }
        }else if component == 1 {
            return self.monthTitlesForYear[row]
        }else {
            return self.yearsForPicker[row]
        }
    }
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        //设置分割线的颜色
        for subView in pickerView.subviews {
            if subView.frame.size.height <= 1 {
                subView.isHidden = false
                subView.backgroundColor = UIColor(red: 236 / 255, green: 237 / 255, blue: 240 / 255, alpha: 1.0)
            }
        }
        let titleLabel = BaseLabel()
        titleLabel.setFont(R.font.sfProTextSemibold(size: 15))
        titleLabel.textColor = UIColor(red: 42 / 255, green: 42 / 255, blue: 42 / 255, alpha: 1.0)
        titleLabel.textAlignment = NSTextAlignment.center
        
        if component == 0 {//day
            if row + 1 < 10 {
                titleLabel.text =  "0\(row + 1)"
            }else{
                titleLabel.text =  "\(row + 1)"
            }
        }else if component == 1 {
            titleLabel.text =  self.monthTitlesForYear[row]
        }else {
            
            titleLabel.text = self.yearsForPicker[row]
            
        }
        return titleLabel
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {//day
            if row + 1 < 10 {
                self.selectDay = "0\(row + 1)"
            }else{
                self.selectDay = "\(row + 1)"
            }
            
        }else if component == 1 {//month
            self.selectMonth = self.monthValuesForYear[row]
            pickerView.reloadComponent(0)
        }else{//year
            self.selectYear = self.yearsForPicker[row]
        }
    }
}
